package com.webull.information.center.carwler.job;

import java.net.InetAddress;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.webull.information.center.carwler.common.model.CollectRecord;
import com.webull.information.center.carwler.common.model.parm.CollectParm;
import com.webull.information.center.carwler.common.util.eljob.ElasticJobUtil;
import com.webull.information.center.carwler.dao.CollectRecordDao;
import com.webull.information.center.carwler.task.News_ReutersGuide_Task2;
import com.webull.securities.client.model.Region;
import com.webull.securities.client.service.RegionService;

/**
 * 路透api新闻采集
 * 
 * @author smj
 * @Date 2017年8月11日上午10:07:42
 * @version 1.0
 *
 */
@Component
public class News_ReutersGuideStock_Job implements SimpleJob {
	protected final Logger logger = LogManager.getLogger(getClass());
	private @Autowired News_ReutersGuide_Task2 news_ReutersGuide_Task2;
	private @Autowired CollectRecordDao collectRecordDao;
	private @Autowired RegionService regionService;
	private final String collectJob = "news_ReutersGuide_stock_Job";


	@Override
	public void execute(ShardingContext context) {
		int sharding = Optional.ofNullable(context).map(v -> v.getShardingItem()).orElse(-1);
		int totalCount = Optional.ofNullable(context).map(v -> v.getShardingTotalCount()).orElse(-1);

		List<Region> all = regionService.searchAll();
		logger.info("News_ReutersGuideStock_Job:" + ArrayUtils.toString(sharding) + ",totalCount:" + totalCount);
		List<Region> shardingRegionIds = Optional.ofNullable(ElasticJobUtil.shardingItems(context, all)).orElse(all);
		logger.info("news_ReutersGuide_Job div shardingRegionIds:{}; ", shardingRegionIds.size());
		if (shardingRegionIds != null && !shardingRegionIds.isEmpty()) {
			process_0(shardingRegionIds);
		}
	}


	public void process_0(List<Region> shardingRegionIds) {
		// Collections.shuffle(shardingRegionIds);
		logger.info("news_ReutersGuide_Job div size{};shardExchanges:{};", ArrayUtils.toString(shardingRegionIds));
		for (Region region : shardingRegionIds) {
			try {
				logger.info("news_ReutersGuide_Job shardExchanges start collectJob{} div ,reginoId:{}",
						collectJob, region.getAlias());
				news_ReutersGuide_Task2.fullReutersGuideStockNews(region);
				CollectRecord record = backRecord(region);
				Integer collId = collectRecordDao.add(record);
				logger.info("news_ReutersGuide_Job shardExchanges end collectJob{} div ,reginoId:{},collId{}",
						collectJob, region.getAlias(), collId);
			} catch (Exception e) {
				logger.error("news_ReutersGuide_Job shardExchanges collectJob div error:" + region, e);
			}

		}
	}


	private CollectParm collectParm(Region region) {
		CollectParm parm = new CollectParm();
		parm.setCollectJob(collectJob);
		parm.setExchangeCode(region.getAlias());
		parm.setExchangeId(region.getId());
		parm.setRecordType(CollectRecord.RecordTypeConstants.NEWS);
		return parm;
	}

	private CollectRecord backRecord(Region region) {
		CollectRecord record = new CollectRecord();
		record.setCollectJob(collectJob);
		record.setExchangeCode(region.getAlias());
		record.setExchangeId(region.getId());
		record.setRecordType(CollectRecord.RecordTypeConstants.NEWS);
		try {
			record.setCustomer(InetAddress.getLocalHost().getHostAddress());
		} catch (Exception e) {
		}
		return record;
	}

}
